//
//  AppDelegate.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/25.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "AppDelegate.h"
#import <AVOSCloud/AVOSCloud.h>
#import <AVOSCloudSNS.h>
#import "RootViewController.h"
#import "PatientData.h"
#import "SeekModel.h"
#import "DataManager.h"
#import <RongCloudIMKit/RongIMKit/RCIM.h>
#import "HospitalModel.h"
#import "Hospital.h"
#import "Medical.h"
#import "Registering.h"
#import <WXApi.h>
#import "WXApiManager.h"


//融云即时通讯AppKey
#define RONGCLOUD_IM_APPKEY @"uwd1c0sxdbad1"
#define CARED_GET_RONGCLOUD_TOKEN_URL @"http://101.200.242.10/rctest/test.php?id=%@&user=%@"
#define kShared [DataManager defaultManager]

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    RootViewController *root = [[RootViewController alloc] init];
    self.window.rootViewController = root;
    [self.window makeKeyAndVisible];
    
    //子类化的注册
    [PatientData registerSubclass];
    [SeekModel registerSubclass];
    [Hospital registerSubclass];
    [Medical registerSubclass];
    [Registering registerSubclass];
    
    //将死数据导入Leancloud，只需要导一次
//    [self makeData];
    //如果使用美国站点，请加上这行代码 [AVOSCloud useAVCloudUS];
    [AVOSCloud setApplicationId:@"MI7uYA3p1SXMFC8fuA3e5I6n-gzGzoHsz"
                      clientKey:@"tyaIHlpTM2swyRwSklXHb9Dc"];
    //融云导入
    [[RCIM sharedRCIM] initWithAppKey:RONGCLOUD_IM_APPKEY];
    [kShared getTokenWithBlock:^(NSString *token) {
        [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
            
        } error:^(RCConnectErrorCode status) {
            
        } tokenIncorrect:^{
            
        }];
    }];
    
    //向微信注册wxd930ea5d5a258f4f
    [WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];

    
    return YES;
}


//将医院的死数据上传到leanCloud上
- (void)makeData
{
    
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Hospital" ofType:@"plist"]];
    for(NSDictionary *dic in array)
    {
        HospitalModel *hospitalModel = [[HospitalModel alloc] init];
        [hospitalModel setValuesForKeysWithDictionary:dic];
        Hospital *hospital = [Hospital object];
        hospital.hospitalName = hospitalModel.hospitalName;
        hospital.doctorCount = hospitalModel.doctorCount;
        hospital.hospitalAddress = hospitalModel.hospitalAddress;
        hospital.hospitalLevel = hospitalModel.hospitalLevel;
        hospital.introduce = hospitalModel.introduce;
        hospital.state = hospitalModel.state;
        hospital.city = hospitalModel.city;
        [hospital saveInBackground];
    }
}


-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    return [AVOSCloudSNS handleOpenURL:url];
}

// When Build with IOS 9 SDK
// For application on system below ios 9
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    return [AVOSCloudSNS handleOpenURL:url];
}
// For application on system equals or larger ios 9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
    return [AVOSCloudSNS handleOpenURL:url];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
